package com.icee.myth.deamon.encoder;

import com.icee.myth.common.encoder.AbsProtobufEncoder;

/**
 * TypeLengthFieldProtobufEncoder用于编码除GW与Map间的服务器间传递消息
 * @author chencheng
 */
public class DeamonToManagerEncoder extends AbsProtobufEncoder implements Cloneable {

    public DeamonToManagerEncoder() {
        super();
    }

//    @Override
//    protected void encode(ChannelHandlerContext ctx, Object msg, List list) throws Exception {
//        if (msg instanceof ProtobufMessage) {
//            list.add(encodeProtobufMessage(ctx.channel(), (ProtobufMessage)msg));
//        } else if (msg instanceof ArrayList) {
//            ArrayList<Object> msgs = (ArrayList<Object>)msg;
//            int size = msgs.size();
//            for(int i=0; i<size; i++) {
//                Object obj = msgs.get(i);
//                if (!(obj instanceof ByteBuf)) {
//                    msgs.set(i, encodeProtobufMessage(ctx.channel(), (ProtobufMessage)msgs.get(i)));
//                }
//            }
//            list.add(NewChannelBuffers.wrappedBuffer(msgs));
//        } else {
//            list.add(msg);
//        }
//    }

//    private Object encodeProtobufMessage(Channel channel, ProtobufMessage pm) {
//        if (pm.payload instanceof MessageLite) {
//            MessageLite ml = (MessageLite) pm.payload;
//            int mlsize = ml.getSerializedSize();
//            byte[] res = new byte[4 + mlsize];
//            res[0] = (byte) ((pm.type >> 8) & 0xFF);
//            res[1] = (byte) (pm.type & 0xFF);
//            res[2] = (byte) ((mlsize >> 8) & 0xFF);
//            res[3] = (byte) (mlsize & 0xFF);
//            CodedOutputStream cos = CodedOutputStream.newInstance(res, 4, mlsize);
//            try {
//                ml.writeTo(cos);
//            } catch (IOException ex) {
//                MLogger.getlogger().log(LogConsts.LOGTYPE_NETERR, StackTraceUtil.getStackTrace(ex));
//            }
//            cos = null;
//            return wrappedBuffer(res);
//        } else if(pm.payload == null) {
//            ByteBuf header = channel.config().getAllocator().buffer(4);
//            header.order(ByteOrder.BIG_ENDIAN);
//            header.writeShort((short)pm.type);
//            header.writeShort((short)0);
//            return header;
//        }
//
//        return pm;
//    }

    @Override
    public Object clone()   {
        DeamonToManagerEncoder o = null;
        try {
            o = (DeamonToManagerEncoder) super.clone();
        } catch (CloneNotSupportedException e) {
            e.printStackTrace();
        }
        return o;
    }
}
